import { type RouteRecordRaw, createRouter, createWebHistory } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"
import { qiankunWindow } from "vite-plugin-qiankun/dist/helper"
const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/quickstart",
    component: Layouts,
    redirect: "/quickstart/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/quick-start/index.vue"),
        name: "QuickStart",
        meta: {
          title: "快速开始",
          svgIcon: "quickstart"
        }
      }
    ]
  },
  {
    path: "/task",
    component: Layouts,
    redirect: "/task/gameindex",
    name: "Task",
    meta: {
      title: "任务列表",
      svgIcon: "tasklist"
    },
    children: [
      {
        path: "gameindex",
        component: () => import("@/views/task/game-task/index.vue"),
        name: "GameTaskList",
        meta: {
          title: "博弈任务管理"
        }
      },
      {
        path: "gamecreate",
        component: () => import("@/views/task/game-task/create/index.vue"),
        name: "GameTaskCreate",
        meta: {
          hidden: true,
          title: "新建博弈任务",
          keepAlive: true
        }
      },
      {
        path: "gameedit",
        component: () => import("@/views/task/game-task/update/index.vue"),
        name: "GameTaskEdit",
        meta: {
          hidden: true,
          title: "编辑博弈任务",
          keepAlive: true
        }
      },
      {
        path: "gameview",
        component: () => import("@/views/task/game-task/view/index.vue"),
        name: "GameTaskView",
        meta: {
          hidden: true,
          title: "查看博弈任务"
        }
      },
      {
        path: "staticindex",
        component: () => import("@/views/task/static-task/index.vue"),
        name: "StaticTaskList",
        meta: {
          title: "检测任务管理"
        }
      },
      {
        path: "staticcreate",
        component: () => import("@/views/task/static-task/create/index.vue"),
        name: "StaticTaskCreate",
        meta: {
          hidden: true,
          title: "新建检测任务",
          keepAlive: true
        }
      },
      {
        path: "staticedit",
        component: () => import("@/views/task/static-task/update/index.vue"),
        name: "StaticTaskEdit",
        meta: {
          hidden: true,
          title: "编辑检测任务",
          keepAlive: true
        }
      },
      {
        path: "staticview",
        component: () => import("@/views/task/static-task/view/index.vue"),
        name: "StaticTaskView",
        meta: {
          hidden: true,
          title: "查看检测任务"
        }
      }
    ]
  },
  {
    path: "/resource",
    component: Layouts,
    redirect: "/resource/source",
    name: "Resource",
    meta: {
      title: "资源管理",
      svgIcon: "resource"
    },
    children: [
      {
        path: "source",
        component: () => import("@/views/resource/source/index.vue"),
        name: "ResourceSourceList",
        meta: {
          title: "源数据资源库"
        }
      },
      {
        path: "game",
        component: () => import("@/views/resource/game/index.vue"),
        name: "ResourceGameList",
        meta: {
          title: "智能博弈资源库"
        }
      },
      {
        path: "scene",
        component: () => import("@/views/resource/scene/index.vue"),
        name: "ResourceSceneList",
        meta: {
          title: "场景库"
        }
      }
    ]
  },
  {
    path: "/algo",
    component: Layouts,
    redirect: "/algo/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/algo-model/index.vue"),
        name: "Algo",
        meta: {
          title: "算法模型库",
          svgIcon: "algo"
        }
      }
    ]
  },
  {
    path: "/eval",
    component: Layouts,
    redirect: "/eval/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/eval-model/index.vue"),
        name: "Eval",
        meta: {
          title: "评估模型库",
          svgIcon: "evalmodeling"
        }
      }
    ]
  }
]
const router = createRouter({
  // routes,
  // history,

  // 判断是否是qiankun环境
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes,
  history: createWebHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? "/toksim" : "")
})
// console.log(history, 'routerrouterrouter')

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
